package com.ocom.human.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.entity.human.EntityPersonnelPalmprint;
import com.ocom.common.request.human.GetPerFeatureRequest;
import com.ocom.common.request.human.PersonQueryRequest;
import com.ocom.common.request.human.PersonQueryVo;
import com.ocom.common.request.human.RegisterPalmRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.human.feignclient.PayConfigClientService;
import com.ocom.human.feignclient.PayHandleFeignService;
import com.ocom.human.service.PersonnelInfoService;
import com.ocom.human.service.PersonnelPalmprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class PersonnelPalmprintController {

    @Autowired
    private PersonnelPalmprintService service;

    @Autowired
    private PersonnelInfoService personnelInfoService;

    @Autowired
    private PayConfigClientService payConfigClientService;

    @Autowired
    private PayHandleFeignService payHandleFeignService;


    @PostMapping(HumanServiceUrl.URL_registerPalm)
    public Result registerPalm(@RequestBody RegisterPalmRequest request) {

        service.deletePalm(request.getComId(), request.getPId(), request.getVersion());

        EntityPersonnelPalmprint entityPersonnelPalmprint = null;

        List<RegisterPalmRequest.Palm> palms = JSON.parseArray(request.getPalmList(), RegisterPalmRequest.Palm.class);
        for (RegisterPalmRequest.Palm palm : palms) {
            entityPersonnelPalmprint = new EntityPersonnelPalmprint();
            entityPersonnelPalmprint.setComId(request.getComId());
            entityPersonnelPalmprint.setPId(request.getPId());
            entityPersonnelPalmprint.setVersion(request.getVersion());
            entityPersonnelPalmprint.setPalm(palm.getPalm());

            service.insertPalm(request.getComId(), entityPersonnelPalmprint);
        }

        payConfigClientService.White(request.getComId(), request.getPId().longValue());

        payHandleFeignService.updateXFDeviceInfo2(request.getComId(), 1, null);

        return ResultUtil.success(request.getPId());
    }

    @PostMapping(HumanServiceUrl.URL_getPersonInfoByPalm)
    public Result getPersonInfoByPalm(@RequestBody PersonQueryRequest request) {
        List<PersonQueryVo> personInfoList = personnelInfoService.getPersonInfoByCardNoAndP(request.getComId(), request.getPId(), request.getPCode(), request.getPName(), request.getCardNo());
        List<Integer> collect = personInfoList.stream().map(PersonQueryVo::getPId).collect(Collectors.toList());
        List<EntityPersonnelPalmprint> palmprints = new ArrayList<>();
        if (null != collect && 0 < collect.size()) {
            palmprints = service.getByPIds(request.getComId(), collect, request.getVersion());

            Map<Integer, List<EntityPersonnelPalmprint>> palmMap = palmprints.stream().collect(Collectors.groupingBy(e -> e.getPId()));
            for (PersonQueryVo person : personInfoList) {
                person.setPalmList(palmMap.get(person.getPId()));
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("personList", personInfoList);
        return ResultUtil.success(map);
    }

    @PostMapping(HumanServiceUrl.URL_getPalmByPIds)
    public Result getPalmByPIds(@RequestBody PersonQueryRequest request) {

        List<EntityPersonnelPalmprint> palmprints = new ArrayList<>();
        if (null != request.getPIds() && 0 < request.getPIds().size()) {
            palmprints = service.getByPIds(request.getComId(), request.getPIds(), request.getVersion());
        }

        return ResultUtil.success(palmprints);
    }
}
